home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / FileLib / FileOpenCloseLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-21  |  2.1 KB  |  81 lines  |  [TEXT/KAHL]

  1. /* Functions for opening and closing files. This follows the guidelines
  2.     published in one of the TechNotes for avoiding the dangers of using
  3.     the reference number of a closed file; when a file is closed, the
  4.     file's reference number is set to the constant FILE_CLOSED.
  5.  
  6.     93/03/12 AIH
  7.     - The volume is flushed after a file is closed
  8.     
  9.     91/11/14 AIH
  10.     - Removed checks for system software version 7.0
  11.     
  12.     91/05/08 AIH
  13.     - HOpenDF is used if it's available
  14.     
  15.     91/04/23 Ari Halberstadt (AIH)
  16.     - Aliases are resolved before a file is opened. */
  17.  
  18. #include "MacLib.h"
  19. #include "FileLib.h"
  20.  
  21. /* open the data fork of the file using the specified permissions */
  22. void FileOpen(FileType *fp, FilePermType perm)
  23. {
  24.     FileRefType ref = FILE_CLOSED;
  25.     OSErr err = noErr;
  26.     
  27.     require(FileValid(fp));
  28.     require(fp->ref == FILE_CLOSED);
  29.  
  30.     /* resolve aliases */
  31.     FileResolve(fp);
  32.  
  33.     /* try to open the file with the specified permission */
  34.     if (MacVersion() >= 0x0700)
  35.         err = HOpenDF(fp->vol, fp->dir, fp->pnm, perm, &ref);
  36.     else
  37.         err = HOpen(fp->vol, fp->dir, fp->pnm, perm, &ref);
  38.  
  39.     /* if the file system doesn't recognize the permission then translate
  40.         it into something the file system is more likely to understand and
  41.         retry */
  42.     if (err == paramErr) {
  43.         if (MacVersion() >= 0x0700)
  44.             err = HOpenDF(fp->vol, fp->dir, fp->pnm, fsCurPerm, &ref);
  45.         else
  46.             err = HOpen(fp->vol, fp->dir, fp->pnm, fsCurPerm, &ref);
  47.     }
  48.     FailOSErr(err);
  49.     fp->ref = ref;
  50.     ensure(fp->ref != FILE_CLOSED);
  51. }
  52.  
  53. /* open the resource fork of the file using the specified permissions */
  54. void FileOpenRes(FileType *fp, FilePermType perm)
  55. {
  56.     FileRefType ref = FILE_CLOSED;
  57.     OSErr err = noErr;
  58.  
  59.     require(FileValid(fp));
  60.     require(fp->ref == FILE_CLOSED);
  61.     FileResolve(fp);
  62.     err = HOpenRF(fp->vol, fp->dir, fp->pnm, perm, &ref);
  63.     if (err == paramErr)
  64.         err = HOpenRF(fp->vol, fp->dir, fp->pnm, fsCurPerm, &ref);
  65.     FailOSErr(err);
  66.     fp->ref = ref;
  67.     ensure(fp->ref != FILE_CLOSED);
  68. }
  69.  
  70. /* close the file */
  71. void FileClose(FileType *fp)
  72. {
  73.     require(FileValid(fp));
  74.     if (fp->ref != FILE_CLOSED) {
  75.         FailOSErr(FSClose(fp->ref));
  76.         fp->ref = FILE_CLOSED;
  77.         FileFlush(fp);
  78.     }
  79.     ensure(fp->ref == FILE_CLOSED);
  80. }
  81.